home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / msgsw.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  8KB  |  331 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *    2nd revision : Feb 1988.
  8.  *
  9.  *    %W%    %G%
  10. */
  11. #include "fig.h"
  12. #include "resources.h"
  13. #include "const.h"
  14. #include "font.h"
  15. #include "paintop.h"
  16.  
  17. extern int        errno;
  18. extern int        receiving_msg;
  19. extern int        action_on;
  20. extern            null_proc();
  21.  
  22. extern int        CANVAS_WIDTH;
  23. extern char        prefix[], suffix[];
  24. extern int        leng_prefix, leng_suffix;
  25. extern int        cur_x, cur_y;
  26.  
  27. extern            (*canvas_kbd_proc)();
  28. extern            (*canvas_locmove_proc)();
  29. extern            (*canvas_leftbut_proc)();
  30. extern            (*canvas_middlebut_proc)();
  31. extern            (*canvas_rightbut_proc)();
  32.  
  33. /*****************  exported  variables and procedures   *****************/
  34.  
  35.             put_msg();
  36.             init_msgreceiving();
  37.  
  38. /************************  local variables and routines ******************/
  39.  
  40. #define            BUF_SIZE        80
  41.  
  42. static            (*msgsw_kbd_proc)();
  43. static            (*msgsw_leftbut_proc)();
  44. static            (*msgsw_rightbut_proc)();
  45. static            (*recipient)();
  46.  
  47. static char        msg_received[BUF_SIZE];
  48. static char        prompt[BUF_SIZE];
  49. static int        len_prompt;
  50. static int        base_x = 5, base_y = 18;
  51. static int        input_base_x;
  52.  
  53. static            msg_selected();
  54. static            msg_sighandler();
  55.  
  56. static            (*saved_canvas_kbd_proc)();
  57. static            (*saved_canvas_locmove_proc)();
  58. static            (*saved_canvas_leftbut_proc)();
  59. static            (*saved_canvas_middlebut_proc)();
  60. static            (*saved_canvas_rightbut_proc)();
  61.  
  62. #ifndef    X11
  63. #ifdef AMIGA
  64. #else
  65. int
  66. init_msg(tool)
  67. TOOL        tool;
  68. {
  69.     INPUTMASK        mask;
  70.  
  71.     msg_sw = tool_createsubwindow(tool, "", -1, -1);
  72.     if (msg_sw == (TOOLSW)0) return(0);
  73.     msgswfd = msg_sw->ts_windowfd;
  74.     (void)fcntl(msgswfd, F_SETFL, fcntl(msgswfd, F_GETFL, 0) | O_NDELAY);
  75.  
  76.     msg_pixwin = pw_open(msgswfd);
  77.     msg_sw->ts_io.tio_selected = msg_selected;
  78.     msg_sw->ts_io.tio_handlesigwinch = msg_sighandler;
  79.     msg_sw->ts_destroy = null_proc;
  80.  
  81.     input_imnull(&mask);
  82.     win_setinputcodebit(&mask, MS_LEFT);
  83.     win_setinputcodebit(&mask, MS_RIGHT);
  84.     mask.im_flags |= IM_ASCII | IM_INTRANSIT;
  85.     win_setinputmask(msgswfd, &mask, NULL, WIN_NULLLINK);
  86.     win_setcursor(msgswfd, &pencil_cursor);
  87.     msgsw_kbd_proc = msgsw_leftbut_proc = msgsw_rightbut_proc = null_proc;
  88.     return(1);
  89.     }
  90. #endif
  91. #else
  92. static Arg      msg_args[] =
  93. {
  94.     { XtNx, (XtArgVal)0 },
  95.     { XtNy, (XtArgVal)0 },
  96.     { XtNwidth, (XtArgVal)0 },
  97.     { XtNheight, (XtArgVal)MSG_HEIGHT },
  98.     { XtNlabel, (XtArgVal)"" },
  99.     { XtNfromHoriz, (XtArgVal)NULL },
  100.     { XtNhorizDistance, (XtArgVal)0 },
  101.     { XtNfromVert, (XtArgVal)NULL },
  102.     { XtNvertDistance, (XtArgVal)0 },
  103. };
  104.  
  105. int init_msg(tool)
  106.     TOOL        tool;
  107. {
  108.     msg_args[2].value = CANVAS_WIDTH;
  109.     msg_args[5].value = (XtArgVal)panel_sw;
  110.     msg_args[7].value = (XtArgVal)canvas_sw;
  111.     msg_sw = XtCreateManagedWidget("message", commandWidgetClass, tool,
  112.         msg_args, XtNumber(msg_args));
  113.     return (1);
  114. }
  115.  
  116. /*
  117. **    We have to do this after realizing the widget otherwise
  118. **    the width is computed wrong and you get a tiny text box.
  119. */
  120. setup_msg()
  121. {
  122.     static Arg      addn_args[] =
  123.     {
  124.         { XtNfont, (XtArgVal)NULL },
  125.     };
  126.  
  127.     if (bold_font != NULL)
  128.     {
  129.         addn_args[0].value = (XtArgVal)bold_font;
  130.         XtSetValues(msg_sw, addn_args, XtNumber(addn_args));
  131.     }
  132.     msgswfd = XtWindow(msg_sw);
  133.     XDefineCursor(tool_d, msgswfd, (Cursor)pencil_cursor.bitmap);
  134. }
  135. #endif    X11
  136.  
  137. #ifndef    X11
  138. #ifdef AMIGA
  139. #else
  140. put_msg(format, arg1, arg2, arg3, arg4, arg5)
  141.     char    *format;
  142.     int    arg1, arg2, arg3, arg4, arg5;
  143. {
  144.     sprintf(prompt, format, arg1, arg2, arg3, arg4, arg5);
  145.     msg_args[4].value = (XtArgVal)prompt;
  146.     XtSetValues(msg_sw, &msg_args[4], 1);
  147. }
  148.  
  149.  
  150. static
  151. msg_selected(nullsw, ibits, obits, ebits, timer)
  152. caddr_t        *nullsw;
  153. int        *ibits, *obits, *ebits;
  154. struct timeval    *timer;
  155. {
  156.     INPUTEVENT       ie;
  157.  
  158.     *ibits = *obits = *ebits = 0;
  159.     if (action_on) return;
  160.     if (input_readevent(msgswfd, &ie) == -1) {
  161.         printf("error %d while read input event in msg window\n", errno);
  162.         return;
  163.         }
  164.     if (win_inputnegevent(&ie)) return;
  165.     if (! receiving_msg) return;
  166.  
  167.     if (ie.ie_code <= ASCII_LAST) {
  168.         msgsw_kbd_proc(ie.ie_code);
  169.         }
  170.     else if (ie.ie_code == MS_LEFT) {
  171.         msgsw_leftbut_proc(ie.ie_locx, ie.ie_locy);
  172.         }
  173.     else if (ie.ie_code == MS_RIGHT) {
  174.         msgsw_rightbut_proc(ie.ie_locx, ie.ie_locy);
  175.         }
  176.     }
  177.  
  178. static
  179. msg_sighandler()
  180. {
  181.     pw_damaged(msg_pixwin);
  182.     pw_writebackground(msg_pixwin, 0, 0, 2048, 2048, PAINT);
  183.     pw_text(msg_pixwin, base_x, base_y, PAINT, bold_font, prompt);
  184.     pw_donedamaged(msg_pixwin);
  185.     }
  186.  
  187. /*VARARGS1*/
  188. put_msg(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  189. char   *format, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6, *arg7, *arg8;
  190. {
  191.     clear_message();
  192.     sprintf(prompt, format, arg1, arg2, arg3, arg4, arg5, 
  193.         arg6, arg7, arg8);
  194.     pw_text(msg_pixwin, base_x, base_y, PAINT, bold_font, prompt);
  195.     }
  196.  
  197. clear_message()
  198. {
  199.     pw_writebackground(msg_pixwin, 0, 0, 2048, 2048, PAINT);
  200.     }
  201. #endif AMIGA
  202. #else
  203. /*VARARGS1*/
  204. put_msg(format, arg1, arg2, arg3, arg4, arg5)
  205.     char    *format;
  206.     int    arg1, arg2, arg3, arg4, arg5;
  207. {
  208.     sprintf(prompt, format, arg1, arg2, arg3, arg4, arg5);
  209.     msg_args[4].value = (XtArgVal)prompt;
  210.     XtSetValues(msg_sw, &msg_args[4], 1);
  211. }
  212.  
  213. clear_message()
  214. {
  215.     msg_args[4].value = (XtArgVal)"";
  216.     XtSetValues(msg_sw, &msg_args[4], 1);
  217. }
  218. #endif    X11
  219.  
  220. static
  221. end_char_input()
  222. {
  223.     receiving_msg = 0;  /*  msg input has been received  */
  224.     terminate_char_handler();
  225.     strcpy(&prefix[leng_prefix], suffix);
  226.     strcpy(msg_received, prefix);
  227.     strcpy(&prompt[len_prompt], prefix);
  228.     if (recipient != NULL) (* recipient) (msg_received);
  229.     restore_event_proc();
  230.     return;
  231.     }
  232.  
  233. static
  234. reposition_cursor(x, y)
  235. int    x, y;
  236. {
  237.     int        leng;
  238.     PR_SIZE        s;
  239.     PR_SIZE pf_textwidth();
  240.  
  241.     strcpy(&prefix[leng_prefix], suffix);
  242.     leng = leng_prefix + leng_suffix; 
  243.     s = pf_textwidth(leng, bold_font, prefix);
  244.     if ( ! (input_base_x <= x && x <= input_base_x+s.x
  245.         && base_y-s.y <= y && y <= base_y)) {
  246.         prefix[leng_prefix] = '\0';
  247.         return;
  248.         }
  249.  
  250.     leng_suffix = 0;
  251.     erase_char_string();
  252.     leng_prefix = prefix_length(bold_font, prefix, x-input_base_x);
  253.     strcpy(suffix, &prefix[leng_prefix]);
  254.     prefix[leng_prefix] = '\0';
  255.     leng_suffix = leng - leng_prefix;
  256.     s = pf_textwidth(leng_prefix, bold_font, prefix);
  257.     cur_x = input_base_x + s.x;
  258.     draw_char_string();
  259.     }
  260.  
  261. #ifndef    X11
  262. #ifdef AMIGA
  263. #else
  264. init_msg_receiving(msg_recipient, p)
  265. int    (*msg_recipient)();
  266. char    *p;            /* prompt */
  267. {
  268.     extern        char_handler();
  269.     PR_SIZE        prompt_size;
  270.  
  271.     saved_canvas_kbd_proc = canvas_kbd_proc;
  272.     saved_canvas_locmove_proc = canvas_locmove_proc;
  273.     saved_canvas_leftbut_proc = canvas_leftbut_proc;
  274.     saved_canvas_middlebut_proc = canvas_middlebut_proc;
  275.     saved_canvas_rightbut_proc = canvas_rightbut_proc;
  276.  
  277.     receiving_msg = 1;
  278.     msgsw_kbd_proc = char_handler;
  279.     msgsw_leftbut_proc = reposition_cursor;
  280.     canvas_kbd_proc = char_handler;
  281.     canvas_locmove_proc = null_proc;
  282.     canvas_leftbut_proc = null_proc;
  283.     canvas_rightbut_proc = null_proc;
  284.     recipient = msg_recipient;
  285.     blink_msg();
  286.     clear_message();
  287.     strcpy(prompt, p);
  288.     pw_text(msg_pixwin, base_x, base_y, PAINT, bold_font, prompt);
  289.  
  290.     len_prompt = strlen(prompt);
  291.     prompt_size = pf_textwidth(len_prompt, bold_font, prompt);
  292.     cur_x = input_base_x = base_x + prompt_size.x;
  293.     cur_y = base_y;
  294.     leng_prefix = leng_suffix = 0;
  295.     *prefix = *suffix = '\0';
  296.     initialize_char_handler(msg_pixwin, bold_font, end_char_input, cur_x, base_y);
  297.     }
  298. #endif
  299. #else
  300. init_msg_receiving(msg_recipient, p)
  301.     int        (*msg_recipient)();
  302.     char        *p;
  303. {
  304.     char        reply[100];
  305.  
  306.     prompt_string(p, reply);
  307.     (*msg_recipient)(reply);
  308. }
  309. #endif    X11
  310.  
  311. restore_event_proc()
  312. {
  313.     canvas_kbd_proc = saved_canvas_kbd_proc;
  314.     canvas_locmove_proc = saved_canvas_locmove_proc;
  315.     canvas_leftbut_proc = saved_canvas_leftbut_proc;
  316.     canvas_middlebut_proc = saved_canvas_middlebut_proc;
  317.     canvas_rightbut_proc = saved_canvas_rightbut_proc;
  318.     }
  319.  
  320. blink_msg()
  321. {
  322.     int    i;
  323.  
  324. #ifndef    X11
  325.     pw_writebackground(msg_pixwin, 0, 0, 2048, 2048, ERASE);
  326. #else
  327. #endif    X11
  328.     for (i = 0; i < 2000; i++);
  329.     clear_message();
  330.     }
  331.